<template>
  <div class="app-container">
    
<br>
<br>
    <el-button type="primary" @click="add">添加诊断模板</el-button>
    <br>
    <br>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    > 
      <el-table-column align="center" label="序号" width="50">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="模板名称" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.templateName }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" align="center" width="300" >
        <template slot-scope="scope">
          <el-button type="primary"  @click="getInfo(scope.row)" size="small">详情</el-button>
          <el-button type="success"  @click="update(scope.row)" size="small">修改</el-button>
          <!-- <el-button type="danger" @click="deleteInfo(scope)" size="small">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="->, total, prev, pager, next"
      :total="this.total"
      :current-page="currentPage"
      :page-size="pageSize"
      @current-change="clickCurPage"
      >
    </el-pagination>
  </div>
</template>

<script>

import { templateList } from '@/api/visitTemplate'

export default {
  name: "VisitTemplateList",
  filters: {

  },
  data() {
    const validateSource = (rule, value, callback) => {
      if (!value) callback(new Error('请输入名称')) 
      else callback()
    }
    return {
      list: null,
      listLoading: true,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      curIndex: -1,
      infoVisible: false,
      formVisible: false,
      info: {},
      form: {},
      dialogTitle: '添加科室模板',
      depts:[],
      formRules: {
        source: [{ required: true, trigger: 'blur', validator: validateSource }],
      },
      jobOfficeEnum: [],
      templateEnum: [],
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      templateList().then(res=>{
        console.log(res)
        this.list = res.data
        this.listLoading = false
      }).catch(err=>{
        console.log("err")
        console.log(err)
        this.listLoading = false
      })
      
    },
    clickCurPage(val){
      this.currentPage = val
      this.fetchData()
    },
    getInfo(row) {
      this.info = row
      row.isInfo = true
      this.$router.push({name: 'VisitTemplate', params: row})
    },
    update(row) {
      this.form = JSON.parse(JSON.stringify(row))
      row.isInfo = false
      this.$router.push({name: 'VisitTemplate', params: row})
    },
    add() {
      this.$router.push({name: 'VisitTemplate'})
    },
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if(this.form.id) {  // update
            console.log(this.form)
            edit(this.form).then(res=>{
              console.log(res)
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.formVisible = false
              this.listLoading = false
              this.$tab.refreshPage({path: '/guidance/visitTemplateList', name: "VisitTemplateList"})
            }).catch(err=>{
              console.log(err)
            })
          } else { // add
            console.log(this.form)
            add(this.form).then(res=>{
              this.formVisible = false
              this.listLoading = false
              this.$tab.refreshPage({path: '/guidance/visitTemplateList', name: "VisitTemplateList"})
            }).catch(err=>{
              console.log(err)
            })
          }
        }
      })
      
    },
    cancel() {
      this.formVisible = false
    },
    reset(){
      this.form = {}
    }
  }
}
</script>
